#!/usr/bin/env python
#conding=utf-8
import getpass
import pickle
"""
验证一个文本里面的密码，如果正确返回，不正确重复3次。
"""
count = 0
def get_passwd():
    try:
        with open('passwd.txt') as f:
           passwd = f.readline().strip()
        return passwd
    except Exception as e:
        raise e
"""
用户注册
"""
def register():
    username = input('请输入用户名:')
    user_passwd = getpass.getpass('请输入密码:')
    try:
        with open('passwd.pkl','rb') as f:
            user_data = pickle.load(f)
    except IOError as e:
        user_data = {}
    except Exception as e:
        raise e
    user_data[username] = user_passwd
    with open('passwd.pkl','wb') as f:
        pickle.dump(user_data, f)
    print('注册成功!')
"""
用户登录
"""
def login():
    while count < 3:
        user_passwd = getpass.getpass('请输入密码:')
        passwd = get_passwd()    
        if user_passwd != passwd:
            count += 1
            print('密码错误，请重试！(最多重试)')
            continue
        print('密码正确')
        break
    else:
        print('请重试 !退出')

if __name__ == '__main__':
"""
开始选择项
""" 
    while True:
          user_choice = input("1 注册,2 登录,0 退出:")
          if user_choice == "1":
             register()
          elif user_choice == "2":
             login()
